﻿using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EWSEditor.CRM.Forms
{
    public partial class ConfigurationDetailsForm : Form
    {

        public ConfigurationDetailsForm()
        {
            InitializeComponent();
            SetupFormAsync();
        }

        private void SetupFormAsync()
        {
            tslblCurrentTime.Text = "Current GMT Time: ";
            panelLoading.Visible = true;
            lblLoading.Visible = true;
            btnRefresh.Enabled = false;
            Cursor = Cursors.WaitCursor;

            Thread RetrieveDataThread = new Thread(new ThreadStart(GatherData));
            RetrieveDataThread.SetApartmentState(ApartmentState.STA);
            RetrieveDataThread.Start();
        }

        private void GatherData()
        {
            try
            {
                OrganizationServiceProxy orgProxy = CrmAuthInfoFactory.GetOrgProxy();
                CrmOrgMailboxCache.RetrieveOrganization(orgProxy);
                CrmOrgMailboxCache.RetrieveAndSetServerProfile(orgProxy);
                CrmOrgMailboxCache.RetrieveUserOrQueueSettings(orgProxy);
            }
            catch (Exception ex)
            {
                this.Invoke(new Action<string, string>(FormUtility.ShowError), ex.StackTrace, "Exception Occurred While Refreshing Data");
                this.Invoke(new Action(RecoverForm));
                return;
            }
            this.Invoke(new Action(SetupForm));
        }

        private void RecoverForm()
        {
            btnRefresh.Enabled = true;
            Cursor = Cursors.Default;
        }

        private void SetupForm()
        {
            FormUtility.BindAttributes(this.TopLevelControl, CrmOrgMailboxCache.Organization, "organization");
            FormUtility.BindAttributes(this.TopLevelControl, CrmOrgMailboxCache.ServerProfile, "emailserverprofile");

            if (CrmOrgMailboxCache.TargetMailboxIsQueue)
            {
                if (CrmOrgMailboxCache.SupportMode)
                {
                    SetMiscDetails();
                    return;
                }
                FormUtility.BindAttributes(this.TopLevelControl, CrmOrgMailboxCache.Queue,"queue");
            }
            else
            {
                FormUtility.BindAttributes(this.TopLevelControl, CrmOrgMailboxCache.TargetUserSettings, "usersettings");
            }

            SetMiscDetails();
        }

        private void SetMiscDetails()
        {
            if (string.IsNullOrEmpty(txtbxenablesmartmatching.Text))
            {
                txtbxenablesmartmatching.Text = "False";
            }

            if (CrmOrgMailboxCache.TargetMailboxIsQueue)
            {
                if (CrmOrgMailboxCache.SupportMode)
                {
                    lbluserincomingemailfilteringmethod.Visible = false;
                    txtbxuserincomingemailfilteringmethod.Visible = false;
                    panelLoading.Visible = false;
                    lblLoading.Visible = false;
                    return;
                }

                lbluserincomingemailfilteringmethod.Visible = false;
                txtbxuserincomingemailfilteringmethod.Visible = false;
                lblqueueincomingemailfilteringmethod.Visible = true;
                txtbxqueueincomingemailfilteringmethod.Visible = true;
                txtbxqueueincomingemailfilteringmethod.Text = MetadataHelpers.GetOptionSetValueLabel("queue", "incomingemailfilteringmethod", ((OptionSetValue)CrmOrgMailboxCache.Queue["incomingemailfilteringmethod"]).Value);
                lblqueueincomingemaildeliverymethod.Visible = true;
                txtbxincomingemaildeliverymethod.Visible = true;
               
            }
            else
            {
                txtbxuserincomingemailfilteringmethod.Text = MetadataHelpers.GetOptionSetValueLabel("usersettings", "incomingemailfilteringmethod", ((OptionSetValue)CrmOrgMailboxCache.TargetUserSettings["incomingemailfilteringmethod"]).Value);
                lblqueueincomingemailfilteringmethod.Visible = false;
                txtbxqueueincomingemailfilteringmethod.Visible = false;
                lblqueueincomingemaildeliverymethod.Visible = false;
                txtbxincomingemaildeliverymethod.Visible = false;
            }

            panelLoading.Visible = false;
            lblLoading.Visible = false;
            btnRefresh.Enabled = true;
            tslblCurrentTime.Text += DateTime.UtcNow.ToString();
            Cursor = Cursors.Default;

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void lblusesslforincoming_Click(object sender, EventArgs e)
        {

        }

        private void txtbxincomingusessl_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            SetupFormAsync();
        }
    }
}
